<?php
/**
 * CakeCMS - 商品管理
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */

App::import ( 'Vendor', 'Taobaotop/TopClient' );
App::import ( 'Vendor', 'Taobaotop/RequestCheckUtil' );
App::import ( 'Vendor', 'Taobaotop/Logger' );
class GoodsController extends AppController {
	public $components = array (
			'CheckUserPermission',
			'AdminLog' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 判断是否登录
		$this->CheckUserPermission->checkLogin ( $this );
		
		$this->set ( 'title_for_layout', 'CakeCMS商品管理' );
	}
	public function index() {
		$this->set ( 'title_for_layout', 'CakeCMS商品管理' );
		
		// 加载css文件
		$this->set ( 'cssList', array (
				'calendar/jscal2.css',
				'calendar/border-radius.css',
				'calendar/win2k.css' 
		) );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'calendar/calendar.js',
				'calendar/lang/en.js',
				'jquery.imagePreview.js' 
		) );
		
		// 通用的树型类
		App::import ( 'Lib', 'Tree', array (
				'file' => '/Lib/Tree.php' 
		) );
		
		$tree = new Tree ();
		$this->loadModel ( "GoodsCate" );
		
		$parentid = $this->params ['parentid'];
		
		$conditions = array ();
		$result = $this->GoodsCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		
		$array = array ();
		foreach ( $result as $val ) {
			$r ['id'] = $val ['GoodsCate'] ['id'];
			$r ['name'] = $val ['GoodsCate'] ['name'];
			$r ['parentid'] = $val ['GoodsCate'] ['parentid'];
			$r ['img'] = $val ['GoodsCate'] ['img'];
			$r ['tags'] = $val ['GoodsCate'] ['tags'];
			$r ['is_hots'] = $val ['GoodsCate'] ['is_hots'];
			$r ['status'] = $val ['GoodsCate'] ['status'];
			$r ['color'] = $val ['GoodsCate'] ['color'];
			$r ['selected'] = $val ['GoodsCate'] ['id'] == $parentid ? 'selected' : '';
			$array [] = $r;
		}
		$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
		$tree->init ( $array );
		$select_categorys = $tree->get_tree ( 0, $str );
		
		$this->set ( 'select_categorys', $select_categorys );
		
		// 接收分页参数
		if (isset ( $this->params ['named'] ['page'] ) && is_numeric ( $this->params ['named'] ['page'] )) {
			if (floor ( $this->params ['named'] ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['named'] ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Goods" );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		$cid = isset ( $this->params ['named'] ['cid'] ) ? $this->params ['named'] ['cid'] : '';
		$is_index = isset ( $this->params ['named'] ['is_index'] ) ? $this->params ['named'] ['is_index'] : '';
		$searchtype = isset ( $this->params ['named'] ['searchtype'] ) ? $this->params ['named'] ['searchtype'] : '';
		$keyword = isset ( $this->params ['named'] ['keyword'] ) ? $this->params ['named'] ['keyword'] : '';
		$start_time = isset ( $this->params ['named'] ['start_time'] ) ? $this->params ['named'] ['start_time'] : '';
		$end_time = isset ( $this->params ['named'] ['end_time'] ) ? $this->params ['named'] ['end_time'] : '';
		
		$this->set ( 'cid', $cid );
		$this->set ( 'is_index', $is_index );
		$this->set ( 'searchtype', $searchtype );
		$this->set ( 'keyword', $keyword );
		$this->set ( 'start_time', $start_time );
		$this->set ( 'end_time', $end_time );
		
		// 整理搜索条件
		$conditions = array ();
		
		if (! empty ( $cid )) {
			$conditions ['cid'] = $cid;
		}
		
		if (! empty ( $is_index )) {
			$conditions ['is_index'] = $is_index;
		}
		
		if ($searchtype == 1) {
			// 标题
			$conditions ['title'] = "LIKE %$keyword%";
		} elseif ($searchtype == 2) {
			// 用户名
			$conditions ['uid'] = $keyword;
		} elseif ($searchtype == 3) {
			// ID
			$conditions ['id'] = $keyword;
		}
		
		if (! empty ( $start_time )) {
			$conditions ['ctime >='] = $start_time;
		}
		if (! empty ( $end_time )) {
			$conditions ['ctime <='] = $end_time;
		}
		
		$list = $this->Goods->GetGoodsByList ( $conditions, $limit );
		
		if ($list) {
			$this->loadModel ( "GoodsCate" );
			
			$this->loadModel ( "BrandCate" );
			
			// 来源
			$sid_config = Configure::read ( 'admin_config.sid' );
			
			foreach ( $list ['result'] as &$val ) {
				if (! empty ( $val ['Goods'] ['title_color'] )) {
					$val ['Goods'] ['title_color'] = "color:title_color;";
				}
				
				$Cate_Info = $this->GoodsCate->GetGoodsCateByID ( $val ['Goods'] ['cid'] );
				$val ['Goods'] ['cate_name'] = $Cate_Info ['GoodsCate'] ['name'];
				
				$Brand_Info = $this->BrandCate->GetBrandCateByID ( $val ['Goods'] ['bid'] );
				$val ['Goods'] ['bid_name'] = $Brand_Info ['BrandCate'] ['name'];
				
				$img_arr = explode ( "_", $val ['Goods'] ['img'] );
				$ext = explode ( ".", $img_arr [1] );
				
				$val ['Goods'] ['img_80'] = "{$img_arr[0]}_80.{$ext[1]}";
				$val ['Goods'] ['img_100'] = "{$img_arr[0]}_100.{$ext[1]}";
				$val ['Goods'] ['img_200'] = "{$img_arr[0]}_200.{$ext[1]}";
				$val ['Goods'] ['img_460'] = "{$img_arr[0]}_460.{$ext[1]}";
				
				$val ['Goods'] ['sid_img'] = "<img width=\"16\" height=\"16\" style=\"margin-right:5px;\" src=\"{$sid_config [$val ['Goods'] ['sid']]}\">";
			}
		}
		
		$this->set ( 'list', $list ['result'] );
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	/**
	 * 分类管理
	 */
	public function cate() {
		$this->set ( 'title_for_layout', '分类管理' );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'goods.js' 
		) );
		
		// 通用的树型类
		App::import ( 'Lib', 'Tree', array (
				'file' => '/Lib/Tree.php' 
		) );
		
		$tree = new Tree ();
		$tree->icon = array (
				'&nbsp;&nbsp;&nbsp;│ ',
				'&nbsp;&nbsp;&nbsp;├─ ',
				'&nbsp;&nbsp;&nbsp;└─ ' 
		);
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		
		$this->loadModel ( "GoodsCate" );
		
		$conditions = array ();
		$result = $this->GoodsCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		
		$array = array ();
		foreach ( $result as $val ) {
			$r ['id'] = $val ['GoodsCate'] ['id'];
			$r ['name'] = $val ['GoodsCate'] ['name'];
			$r ['img'] = $val ['GoodsCate'] ['img'];
			$r ['parentid'] = $val ['GoodsCate'] ['parentid'];
			$r ['tags'] = $val ['GoodsCate'] ['tags'];
			$r ['is_hots'] = $val ['GoodsCate'] ['is_hots'];
			$r ['status'] = $val ['GoodsCate'] ['status'];
			$r ['listorder'] = $val ['GoodsCate'] ['listorder'];
			$r ['color'] = $val ['GoodsCate'] ['color'];
			$r ['str_manage'] = '<a href="/Goods/addCate/' . $val ['GoodsCate'] ['id'] . '/">添加子分类</a> | ';
			$r ['str_manage'] .= '<a href="/Goods/editCate/' . $val ['GoodsCate'] ['id'] . '/">修改</a> | ';
			$r ['str_manage'] .= '<a href="javascript:;" onclick="cate_delete(' . $val ['GoodsCate'] ['id'] . ')">删除</a>';
			$array [] = $r;
		}
		
		$str = "<tr>
		<td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
		<td align='center'>\$id</td>
		<td >\$spacer\$name</td>
		<td align='center'>\$str_manage</td>
		</tr>";
		
		$tree->init ( $array );
		$select_categorys = $tree->get_tree ( 0, $str );
		$this->set ( 'categorys', $select_categorys );
	}
	/**
	 * 添加分类
	 */
	public function addCate() {
		if ($this->request->isPost ()) {
			$data ['GoodsCate'] ['name'] = $this->data ['name'];
			if (empty ( $data ['GoodsCate'] ['name'] )) {
				$this->flash ( '分类名称不能为空!', '/Goods/addCate/', 1250 );
				return false;
			}
			$data ['GoodsCate'] ['img'] = $this->data ['img'];
			$data ['GoodsCate'] ['parentid'] = $this->data ['parentid'];
			if (! isset ( $data ['GoodsCate'] ['parentid'] )) {
				$this->flash ( '上级分类不能为空!', '/Goods/addCate/', 1250 );
				return false;
			}
			$data ['GoodsCate'] ['tags'] = $this->data ['tags'];
			$data ['GoodsCate'] ['is_hots'] = $this->data ['is_hots'] == 1 ? 1 : 0;
			$data ['GoodsCate'] ['status'] = $this->data ['status'] == 1 ? 1 : 0;
			$data ['GoodsCate'] ['color'] = $this->data ['color'];
			
			$this->loadModel ( 'GoodsCate' );
			if ($this->GoodsCate->add ( $data )) {
				
				// 记录操作日志
				$this->AdminLog->addLog ( "添加商品分类\"{$data ['GoodsCate'] ['name']}\"" );
				
				$this->flash ( '添加商品分类成功!', '/Goods/cate/', 1250 );
				return false;
			} else {
				$this->flash ( '添加商品分类失败!', '/Goods/addCate/', 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', '添加商品分类' );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'goods.js',
					'colorpicker.js' 
			) );
			
			// 通用的树型类
			App::import ( 'Lib', 'Tree', array (
					'file' => '/Lib/Tree.php' 
			) );
			
			$tree = new Tree ();
			$this->loadModel ( "GoodsCate" );
			
			$parentid = $this->params ['parentid'];
			
			$conditions = array ();
			$result = $this->GoodsCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			
			$array = array ();
			foreach ( $result as $val ) {
				$r ['id'] = $val ['GoodsCate'] ['id'];
				$r ['name'] = $val ['GoodsCate'] ['name'];
				$r ['parentid'] = $val ['GoodsCate'] ['parentid'];
				$r ['img'] = $val ['GoodsCate'] ['img'];
				$r ['tags'] = $val ['GoodsCate'] ['tags'];
				$r ['is_hots'] = $val ['GoodsCate'] ['is_hots'];
				$r ['status'] = $val ['GoodsCate'] ['status'];
				$r ['color'] = $val ['GoodsCate'] ['color'];
				$r ['selected'] = $val ['GoodsCate'] ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$this->set ( 'select_categorys', $select_categorys );
		}
	}
	
	/**
	 * 编辑分类
	 */
	public function editCate() {
		if ($this->request->isPost ()) {
			$data ['GoodsCate'] ['name'] = $this->data ['name'];
			if (empty ( $data ['GoodsCate'] ['name'] )) {
				$this->flash ( '分类名称不能为空!', '/Goods/addCate/', 1250 );
				return false;
			}
			$data ['GoodsCate'] ['img'] = $this->data ['img'];
			$data ['GoodsCate'] ['parentid'] = $this->data ['parentid'];
			if (! isset ( $data ['GoodsCate'] ['parentid'] )) {
				$this->flash ( '上级分类不能为空!', '/Goods/addCate/', 1250 );
				return false;
			}
			$data ['GoodsCate'] ['tags'] = $this->data ['tags'];
			$data ['GoodsCate'] ['is_hots'] = $this->data ['is_hots'] == 1 ? 1 : 0;
			$data ['GoodsCate'] ['status'] = $this->data ['status'] == 1 ? 1 : 0;
			$data ['GoodsCate'] ['color'] = $this->data ['color'];
			
			$data ['GoodsCate'] ['id'] = $this->data ['id'];
			
			$this->loadModel ( 'GoodsCate' );
			if ($this->GoodsCate->edit ( $data )) {
				
				// 记录操作日志
				$this->AdminLog->addLog ( "编辑商品分类\"{$data ['GoodsCate'] ['name']}\"" );
				
				$this->flash ( '编辑商品分类成功!', '/Goods/cate/', 1250 );
				return false;
			} else {
				$this->flash ( '编辑商品分类失败!', "/Goods/editCate/{$this->data ['id']}/", 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', '编辑添加商品分类' );
			
			$id = $this->params ['id'];
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'goods.js',
					'colorpicker.js' 
			) );
			
			// 通用的树型类
			App::import ( 'Lib', 'Tree', array (
					'file' => '/Lib/Tree.php' 
			) );
			
			$tree = new Tree ();
			$this->loadModel ( "GoodsCate" );
			
			$parentid_Info = $this->GoodsCate->GetGoodsCateByID ( $id );
			$parentid = $parentid_Info ['GoodsCate'] ['parentid'];
			
			$conditions = array ();
			$result = $this->GoodsCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			
			$array = array ();
			foreach ( $result as $val ) {
				$r ['id'] = $val ['GoodsCate'] ['id'];
				$r ['name'] = $val ['GoodsCate'] ['name'];
				$r ['parentid'] = $val ['GoodsCate'] ['parentid'];
				$r ['img'] = $val ['GoodsCate'] ['img'];
				$r ['tags'] = $val ['GoodsCate'] ['tags'];
				$r ['is_hots'] = $val ['GoodsCate'] ['is_hots'];
				$r ['status'] = $val ['GoodsCate'] ['status'];
				$r ['color'] = $val ['GoodsCate'] ['color'];
				$r ['selected'] = $val ['GoodsCate'] ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$goods_info = $this->GoodsCate->GetGoodsCateByID ( $id );
			$this->set ( 'Goods_info', $goods_info );
			$this->set ( 'select_categorys', $select_categorys );
			$this->set ( 'id', $id );
		}
	}
	/**
	 * 删除分类
	 */
	public function deleteCate() {
		$id = $this->data ['id'];
		
		if (empty ( $id )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '参数错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "Goods" );
		$conditions = array (
				'cid' => $id 
		);
		$count = $this->Goods->find ( 'count', array (
				'conditions' => $conditions 
		) );
		
		if ($count > 0) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '请先删除商品数据!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "GoodsCate" );
		if ($this->GoodsCate->del ( $id )) {
			
			// 记录操作日志
			$this->AdminLog->addLog ( '删除商品分类,ID:' . $id );
			
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
	/**
	 * 获取淘宝url地址id
	 *
	 * @param string $url        	
	 * @return Ambigous <number, unknown>
	 */
	private function get_id($url) {
		$id = 0;
		$parse = parse_url ( $url );
		if (isset ( $parse ['query'] )) {
			parse_str ( $parse ['query'], $params );
			if (isset ( $params ['id'] )) {
				$id = $params ['id'];
			} elseif (isset ( $params ['item_id'] )) {
				$id = $params ['item_id'];
			} elseif (isset ( $params ['default_item_id'] )) {
				$id = $params ['default_item_id'];
			}
		}
		return $id;
	}
	/**
	 * 添加商品
	 */
	public function add() {
		if ($this->request->isPost ()) {
			$data ['Goods'] ['cid'] = $this->data ['cid'];
			if (empty ( $data ['Goods'] ['cid'] )) {
				$this->flash ( '请选择商品分类!', '/Goods/add/', 1250 );
				return false;
			}
			$data ['Goods'] ['bid'] = $this->data ['bid'];
			if (empty ( $data ['Goods'] ['bid'] )) {
				$this->flash ( '请选择品牌分类!', '/Goods/add/', 1250 );
				return false;
			}
			$data ['Goods'] ['sid'] = $this->data ['sid'];
			if (empty ( $data ['Goods'] ['sid'] )) {
				$this->flash ( '请选择来源!', '/Goods/add/', 1250 );
				return false;
			}
			$data ['Goods'] ['uid'] = $this->data ['uid'];
			if (empty ( $data ['Goods'] ['uid'] )) {
				$this->flash ( '请选择马甲!', '/Goods/add/', 1250 );
				return false;
			}
			
			$data ['Goods'] ['title'] = $this->data ['title'];
			if (empty ( $data ['Goods'] ['title'] )) {
				$this->flash ( '商品名称不能为空!', '/Goods/add/', 1250 );
				return false;
			}
			$data ['Goods'] ['title_color'] = $this->data ['title_color'];
			$data ['Goods'] ['img'] = $this->data ['img'];
			$data ['Goods'] ['old_price'] = $this->data ['old_price'];
			$data ['Goods'] ['price'] = $this->data ['price'];
			$data ['Goods'] ['url'] = $this->data ['url'];
			if (empty ( $data ['Goods'] ['url'] )) {
				$this->flash ( '链接地址不能为空!', '/Goods/add/', 1250 );
				return false;
			}
			// ********************************
			// //调用淘宝接口生成转换链接
			// ********************************
			// 获取地址url的ID
			$id = $this->get_id ( $this->data ['url'] );
			
			// 淘宝客PID
			$taobao_app_pid = Configure::read ( 'admin_config.taobao_app_pid' );
			$taobao_app_key = Configure::read ( 'admin_config.taobao_app_key' );
			$taobao_app_secret = Configure::read ( 'admin_config.taobao_app_secret' );
			
			// 这部份是取从taobao获取商品信息
			$tb_top = new TopClient ();
			$tb_top->appkey = $taobao_app_key;
			$tb_top->secretKey = $taobao_app_secret;
			
			// $req = $tb_top->load_api ( 'ItemGetRequest' );
			// $req->setFields (
			// "detail_url,num_iid,title,nick,pic_url,cid,price,type,delist_time,post_fee,score,volume"
			// );
			// $req->setNumIid ( $id );
			// $resp = $tb_top->execute ( $req );
			// $res = ( array ) $resp;
			
			if ($taobao_app_pid && ! empty ( $taobao_app_pid )) {
				$req = $tb_top->load_api ( 'TaobaokeItemsDetailGetRequest' );
				$req->setFields ( "click_url" );
				$req->setNumIids ( $id );
				$req->setPid ( $taobao_app_pid );
				$resp = $tb_top->execute ( $req );
				
				if (isset ( $resp->taobaoke_item_details )) {
					$taoke = ( array ) $resp->taobaoke_item_details->taobaoke_item_detail;
					if ($taoke ['click_url']) {
						$data ['Goods'] ['click_url'] = $taoke ['click_url'];
					}
				}
			}
			
			// **********调用淘宝接口生成转换链接***********
			// **************************************
			$data ['Goods'] ['content'] = $this->data ['content'];
			if (! isset ( $data ['Goods'] ['content'] )) {
				$this->flash ( '商品详情不能为空!', '/Goods/add/', 1250 );
				return false;
			}
			$data ['Goods'] ['hits'] = 0;
			$data ['Goods'] ['likes'] = 0;
			$data ['Goods'] ['haves'] = 0;
			$data ['Goods'] ['comments'] = 0;
			$data ['Goods'] ['is_index'] = $this->data ['is_index'] == 1 ? 1 : 0;
			$data ['Goods'] ['is_hots'] = $this->data ['is_hots'] == 1 ? 1 : 0;
			$data ['Goods'] ['status'] = $this->data ['status'] == 1 ? 1 : 0;
			$data ['Goods'] ['remark'] = $this->data ['remark'];
			$data ['Goods'] ['ctime'] = date ( "Y-m-d H:i:s", time () );
			
			$this->loadModel ( 'Goods' );
			if ($this->Goods->add ( $data )) {
				
				if (isset ( $this->data ['dosubmit'] )) {
					$this->flash ( '添加商品成功！', "/blank/", 1250 );
					return false;
				}
				
				if (isset ( $this->data ['dosubmit_continue'] )) {
					$this->flash ( '添加商品成功！!', "/Goods/add/", 1250 );
					return false;
				}
			} else {
				$this->flash ( '添加商品失败!', '/Goods/add/', 1250 );
				return false;
			}
		} else {
			// 加载css文件
			$this->set ( 'cssList', array (
					'kindeditor/themes/default/default.css',
					'kindeditor/plugins/code/prettify.css' 
			) );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'jquery.cookie.js',
					'colorpicker.js',
					'kindeditor/kindeditor.js',
					'kindeditor/lang/zh_CN.js',
					'kindeditor/plugins/code/prettify.js',
					'goods.js' 
			) );
			
			// 通用的树型类
			App::import ( 'Lib', 'Tree', array (
					'file' => '/Lib/Tree.php' 
			) );
			
			$tree = new Tree ();
			$this->loadModel ( "GoodsCate" );
			
			$parentid = $this->params ['parentid'];
			
			$conditions = array ();
			$result = $this->GoodsCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			
			$array = array ();
			foreach ( $result as $val ) {
				$r ['id'] = $val ['GoodsCate'] ['id'];
				$r ['name'] = $val ['GoodsCate'] ['name'];
				$r ['parentid'] = $val ['GoodsCate'] ['parentid'];
				$r ['img'] = $val ['GoodsCate'] ['img'];
				$r ['tags'] = $val ['GoodsCate'] ['tags'];
				$r ['is_hots'] = $val ['GoodsCate'] ['is_hots'];
				$r ['status'] = $val ['GoodsCate'] ['status'];
				$r ['color'] = $val ['GoodsCate'] ['color'];
				$r ['selected'] = $val ['GoodsCate'] ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$this->set ( 'select_categorys', $select_categorys );
			
			// ----------------------------------------------------
			// 马甲用户
			$majia_config = Configure::read ( 'admin_config.majia' );
			
			$this->loadModel ( "User" );
			// 整理搜索条件
			$conditions = array (
					'uid' => $majia_config 
			);
			
			$list = $this->User->GetUserByList ( $conditions );
			$this->set ( 'majia_list', $list ['result'] );
			// --------------------------------------------------
			
			// 品牌分类
			$this->loadModel ( "BrandCate" );
			
			$conditions = array (
					'parentid' => 0 
			);
			$result = $this->BrandCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			
			$brand_array = array ();
			
			foreach ( $result as $val ) {
				$r ['id'] = $val ['BrandCate'] ['id'];
				$r ['name'] = $val ['BrandCate'] ['name'];
				$r ['parentid'] = $val ['BrandCate'] ['parentid'];
				$r ['img'] = $val ['BrandCate'] ['img'];
				$r ['tags'] = $val ['BrandCate'] ['tags'];
				$r ['is_hots'] = $val ['BrandCate'] ['is_hots'];
				$r ['status'] = $val ['BrandCate'] ['status'];
				$r ['color'] = $val ['BrandCate'] ['color'];
				$r ['selected'] = '';
				$brand_array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init ( $brand_array );
			$brand_categorys = $tree->get_tree ( 0, $str );
			
			$this->set ( 'brand_categorys', $brand_categorys );
		}
	}
	/**
	 * 编辑商品
	 */
	public function edit() {
		if ($this->request->isPost ()) {
			$data ['Goods'] ['cid'] = $this->data ['cid'];
			if (empty ( $data ['Goods'] ['cid'] )) {
				$this->flash ( '请选择商品分类!', "/Goods/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			$data ['Goods'] ['bid'] = $this->data ['bid'];
			if (empty ( $data ['Goods'] ['bid'] )) {
				$this->flash ( '请选择品牌分类!', "/Goods/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			$data ['Goods'] ['sid'] = $this->data ['sid'];
			if (empty ( $data ['Goods'] ['sid'] )) {
				$this->flash ( '请选择来源!', "/Goods/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			$data ['Goods'] ['uid'] = $this->data ['uid'];
			if (empty ( $data ['Goods'] ['uid'] )) {
				$this->flash ( '请选择马甲!', "/Goods/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			
			$data ['Goods'] ['title'] = $this->data ['title'];
			if (empty ( $data ['Goods'] ['title'] )) {
				$this->flash ( '商品名称不能为空!', "/Goods/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			$data ['Goods'] ['title_color'] = $this->data ['title_color'];
			$data ['Goods'] ['img'] = $this->data ['img'];
			$data ['Goods'] ['old_price'] = $this->data ['old_price'];
			$data ['Goods'] ['price'] = $this->data ['price'];
			$data ['Goods'] ['url'] = $this->data ['url'];
			if (empty ( $data ['Goods'] ['url'] )) {
				$this->flash ( '链接地址不能为空!', "/Goods/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			// ********************************
			// //调用淘宝接口生成转换链接
			// ********************************
			// 获取地址url的ID
			$id = $this->get_id ( $this->data ['url'] );
			
			// 淘宝客PID
			$taobao_app_pid = Configure::read ( 'admin_config.taobao_app_pid' );
			$taobao_app_key = Configure::read ( 'admin_config.taobao_app_key' );
			$taobao_app_secret = Configure::read ( 'admin_config.taobao_app_secret' );
			
			// 这部份是取从taobao获取商品信息
			$tb_top = new TopClient ();
			$tb_top->appkey = $taobao_app_key;
			$tb_top->secretKey = $taobao_app_secret;
			
			// $req = $tb_top->load_api ( 'ItemGetRequest' );
			// $req->setFields (
			// "detail_url,num_iid,title,nick,pic_url,cid,price,type,delist_time,post_fee,score,volume"
			// );
			// $req->setNumIid ( $id );
			// $resp = $tb_top->execute ( $req );
			// $res = ( array ) $resp;
			
			if ($taobao_app_pid && ! empty ( $taobao_app_pid )) {
				$req = $tb_top->load_api ( 'TaobaokeItemsDetailGetRequest' );
				$req->setFields ( "click_url" );
				$req->setNumIids ( $id );
				$req->setPid ( $taobao_app_pid );
				$resp = $tb_top->execute ( $req );
				
				if (isset ( $resp->taobaoke_item_details )) {
					$taoke = ( array ) $resp->taobaoke_item_details->taobaoke_item_detail;
					if ($taoke ['click_url']) {
						$data ['Goods'] ['click_url'] = $taoke ['click_url'];
					}
				}
			}
			
			// **********调用淘宝接口生成转换链接***********
			// **************************************
			$data ['Goods'] ['content'] = $this->data ['content'];
			if (! isset ( $data ['Goods'] ['content'] )) {
				$this->flash ( '商品详情不能为空!', "/Goods/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			$data ['Goods'] ['hits'] = 0;
			$data ['Goods'] ['likes'] = 0;
			$data ['Goods'] ['haves'] = 0;
			$data ['Goods'] ['comments'] = 0;
			$data ['Goods'] ['is_index'] = $this->data ['is_index'] == 1 ? 1 : 0;
			$data ['Goods'] ['is_hots'] = $this->data ['is_hots'] == 1 ? 1 : 0;
			$data ['Goods'] ['status'] = $this->data ['status'] == 1 ? 1 : 0;
			$data ['Goods'] ['remark'] = $this->data ['remark'];
			$data ['Goods'] ['ctime'] = date ( "Y-m-d H:i:s", time () );
			
			$data ['Goods'] ['id'] = $this->data ['id'];
			
			$this->loadModel ( 'Goods' );
			if ($this->Goods->edit ( $data )) {
				
				if (isset ( $this->data ['dosubmit'] )) {
					$this->flash ( '编辑商品成功！', "/blank/", 1250 );
					return false;
				}
				
				if (isset ( $this->data ['dosubmit_continue'] )) {
					$this->flash ( '编辑商品成功！!', "/Goods/add/", 1250 );
					return false;
				}
			} else {
				$this->flash ( '编辑商品失败!', "/Goods/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
		} else {
			// 加载css文件
			$this->set ( 'cssList', array (
					'kindeditor/themes/default/default.css',
					'kindeditor/plugins/code/prettify.css' 
			) );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'jquery.cookie.js',
					'colorpicker.js',
					'kindeditor/kindeditor.js',
					'kindeditor/lang/zh_CN.js',
					'kindeditor/plugins/code/prettify.js',
					'goods.js' 
			) );
			
			$id = $this->params ['id'];
			
			// 通用的树型类
			App::import ( 'Lib', 'Tree', array (
					'file' => '/Lib/Tree.php' 
			) );
			
			$tree = new Tree ();
			$this->loadModel ( "GoodsCate" );
			$this->loadModel ( "Goods" );
			
			$Goods_info = $this->Goods->GetGoodsByID ( $id );
			$cid = $Goods_info ['Goods'] ['cid'];
			$bid = $Goods_info ['Goods'] ['bid'];
			
			$conditions = array ();
			$result = $this->GoodsCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			
			$array = array ();
			foreach ( $result as $val ) {
				$r ['id'] = $val ['GoodsCate'] ['id'];
				$r ['name'] = $val ['GoodsCate'] ['name'];
				$r ['parentid'] = $val ['GoodsCate'] ['parentid'];
				$r ['img'] = $val ['GoodsCate'] ['img'];
				$r ['tags'] = $val ['GoodsCate'] ['tags'];
				$r ['is_hots'] = $val ['GoodsCate'] ['is_hots'];
				$r ['status'] = $val ['GoodsCate'] ['status'];
				$r ['color'] = $val ['GoodsCate'] ['color'];
				$r ['selected'] = $val ['GoodsCate'] ['id'] == $cid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$this->set ( 'Goods_info', $Goods_info );
			$this->set ( 'select_categorys', $select_categorys );
			
			// ----------------------------------------------------
			// 马甲用户
			$majia_config = Configure::read ( 'admin_config.majia' );
			
			$this->loadModel ( "User" );
			// 整理搜索条件
			$conditions = array (
					'uid' => $majia_config 
			);
			
			$list = $this->User->GetUserByList ( $conditions );
			$this->set ( 'majia_list', $list ['result'] );
			// --------------------------------------------------
			
			// 品牌分类
			$this->loadModel ( "BrandCate" );
			
			$parentid_Info = $this->BrandCate->GetBrandCateByID ( $bid );
			$parentid = $parentid_Info ['BrandCate'] ['parentid'];
			
			$parentid_Info = $this->BrandCate->GetBrandCateByID ( $parentid );
			$parentid = $parentid_Info ['BrandCate'] ['parentid'];
			
			$conditions = array (
					'parentid' => 0 
			);
			$result = $this->BrandCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			
			$brand_array = array ();
			foreach ( $result as $val ) {
				$r ['id'] = $val ['BrandCate'] ['id'];
				$r ['name'] = $val ['BrandCate'] ['name'];
				$r ['parentid'] = $val ['BrandCate'] ['parentid'];
				$r ['img'] = $val ['BrandCate'] ['img'];
				$r ['tags'] = $val ['BrandCate'] ['tags'];
				$r ['is_hots'] = $val ['BrandCate'] ['is_hots'];
				$r ['status'] = $val ['BrandCate'] ['status'];
				$r ['color'] = $val ['BrandCate'] ['color'];
				$r ['selected'] = $val ['BrandCate'] ['id'] == $parentid ? 'selected' : '';
				$brand_array [] = $r;
			}
			
			$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init ( $brand_array );
			$brand_categorys = $tree->get_tree ( 0, $str );
			$this->set ( 'brand_categorys', $brand_categorys );
			
			// 基础分类
			$conditions = array (
					'parentid' => $parentid 
			);
			$result = $this->BrandCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			
			$parentid_Info = $this->BrandCate->GetBrandCateByID ( $bid );
			$parentid = $parentid_Info ['BrandCate'] ['parentid'];
			
			$basic_str = "";
			foreach ( $result as $val ) {
				$selected = '';
				if ($val ['BrandCate'] ['id'] == $parentid) {
					$selected = 'selected';
				}
				$basic_str .= "<option value='{$val ['BrandCate'] ['id']}' {$selected}>{$val ['BrandCate'] ['name']}</option>";
			}
			$this->set ( 'basic_categorys', $basic_str );
			
			// 品牌
			$conditions = array (
					'parentid' => $parentid 
			);
			$result = $this->BrandCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			
			$bid_str = "";
			foreach ( $result as $val ) {
				$selected = '';
				if ($val ['BrandCate'] ['id'] == $bid) {
					$selected = 'selected';
				}
				$bid_str .= "<option value='{$val ['BrandCate'] ['id']}' {$selected}>{$val ['BrandCate'] ['name']}</option>";
			}
			$this->set ( 'bid_categorys', $bid_str );
		}
	}
}
?>